<?php

namespace app\controllers;

use app\models\Message;
use Yii;
use yii\base\UserException;
use yii\web\NotFoundHttpException;

class SiteController extends BaseController {
    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'app\controllers\ErrorAction',
            ],
        ];
    }

    public function  actionIndex(){
        if(!$this->check([], false)) {
            return $this->redirect('/site/login');
        }
        return $this->redirect('/news/index');
    }

    public function actionLogin($message = '') {
        $this->layout = false;
        Yii::$app->user->logout();
        return $this->render('login', ['message' => $message,]);
    }

    public function actionMessage($id) {
        $user = $this->check();
        $message = Message::findOne($id);
        if(!$message) {
            throw new UserException('该消息不存在');
        }
        if($message->user_id != $user->id) {
            throw new UserException('该消息不存在');
        }
        $message->if_read = true;
        $message->save();
        $this->log('新闻 - 查询列表', '');
        return $this->redirect($message->link);
    }

    public function actionAll($name0, $name1) {
        if(!$this->check([], false)) {
            return $this->redirect('/site/login');
        }
        $name = "/$name0/$name1.php";
        $viewFile = Yii::getAlias($name);
        // var_dump($viewFile);exit;
        // if (!is_file($viewFile)) {
        //     throw new NotFoundHttpException('未找到URL');
        // }
        return $this->render($name, []);
    }
}
